<?php

namespace catchAdmin\model;

use catcher\exceptions\AlertErrorException;

class ZhaoPin extends ModelApp
{

    protected $append = [
        'audit_status_name',
        'audit_time_show',
    ];


    ############################ 关系 start ###############################

    public function wechatUser()
    {
        return $this
            ->hasOne(WechatUser::class, 'id', 'wechat_user_id')
            ->bind([
                'username' => 'nickname',
//                'company_name' => 'company_name',
            ]);
    }


    ############################ 关系 end ###############################
    ############################ Attr start ###############################

    /**
     * audit_time_show
     */
    public function getAuditTimeShowAttr()
    {
        if (empty($this->getOrigin('audit_time'))) {
            return "——";
        }

        return date('Y-m-d H:i:s', $this->getOrigin('audit_time'));
    }


    /**
     * audit_status_name
     */
    public function getAuditStatusNameAttr()
    {
        $list = [
            '1' => '审核中',
            '2' => '通过',
            '3' => '拒绝',
        ];

        return $list[$this->getOrigin('audit_status')] ?? "";
    }
    ############################ Attr end ##########################
    ############################ Search start ##########################

    public function searchContentAttr($query, $value, $data)
    {
        $query->where('content', 'like', '%' . $value . '%');
    }


    public function searchTitleAttr($query, $value, $data)
    {
        $query->where('title', 'like', '%' . $value . '%');
    }


    ############################ Search end ##########################

    public function saveDataApp($param)
    {
        if (!$this->getCompanyIs()) {
            throw new AlertErrorException('未通过公司审核，不能发布招聘');
        }

        $msg_arr = [
            'id'            => $param['id'] ?? 0,
            'company_name'  => $param['company_name'] ?? '',
            'job_type'      => $param['job_type'] ?? 1,
            'title'         => $param['title'] ?? '',
            'content'       => $param['content'] ?? '',
            'history_need'  => $param['history_need'] ?? '',
            'xue_li_need'   => $param['xue_li_need'] ?? '',
            'salary'        => $param['salary'] ?? '',
            'show_end_date' => $param['show_end_date'] ?? '',

            'wechat_user_id' => $this->getLoginId(),
            'audit_status'   => 1,
            'audit_note'     => '',
            'audit_time'     => 0,
        ];

        return parent::saveDataCC($msg_arr);
    }
}
